/**
 * 
 */
package implementation;

import java.awt.Graphics;

import javax.swing.JPanel;

/**
* <br><b>GraphPannel</b>s Custom panel used for graphical representation of the function
* </br>
* <b>Implements</b> ActionListener
* @author NextEpisode1
* @version 1.6
* @since 29/4/2011
*/
public class GraphPannel extends JPanel {
	private static final long serialVersionUID = 1L;
	public static boolean drawGraph;
	private MainForm parrent;

	/**
	 * @param _parent frame - the parent window of the pannel
	 */
	public GraphPannel(MainForm _parrent) {
		super();
		parrent=_parrent;
	}
	
	/**
	 * Custom paintComponentFunction
	 * @param Graphics g
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		int fx=0;
		int fy=0;
		if (drawGraph)
			for (Integer i=0;i<getWidth();i+=5) {
				int loopContor=0;
				TokenStacks s = new TokenStacks();
				s.parseExpression(parrent.getFuncField().getText().replace("x",i.toString()));
				Double y = s.createTree().computeDerivate();
				while (Math.abs(y)<10 && loopContor<100){
					y=y*10;
					loopContor++;
				}
				g.drawLine(fx, fy, i, getHeight()/2-y.intValue());
				System.out.println(fy);
				fx=i;
				fy=getHeight()/2-y.intValue();
			}
		
	}

}
